package com.primb.bench.common.module.repository;

import com.primb.baselibs.util.FormatUtils;
import com.primb.bench.common.module.http.service.ApiClient;
import com.primb.bench.common.module.http.service.ApiService;
import com.primb.bench.project.home.entity.ClientInfoEntity;
import com.primb.bench.project.home.entity.EnsureInfoEntity;
import com.primb.bench.project.home.entity.HttpInfoEntity;
import com.primb.bench.project.home.entity.HttpLoanTreeEntity;
import com.primb.bench.project.home.entity.HttpLogoEntity;
import com.primb.bench.project.home.entity.HttpStockEntity;
import com.primb.bench.project.home.entity.LoanCalculateInfoEntity;
import com.primb.bench.project.home.entity.LoanInfoEntity;
import com.primb.bench.project.home.entity.LogoInfoEntity;
import com.primb.bench.project.home.entity.TaxInfoEntity;
import com.primb.bench.project.home.model.AddInfoModelImpl;
import com.primb.netlibs.converter.RxSchedulerUtils;
import com.primb.netlibs.exception.ApiException;

import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;

import rx.Observable;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.functions.Func3;
import rx.functions.Func6;

import static com.primb.bench.common.constant.AppConstant.USER_NAME;
import static com.primb.bench.common.constant.AppConstant.USER_NAME_ID;
import static com.primb.bench.common.constant.AppConstant.USER_PART_ID;
import static com.primb.bench.common.constant.AppConstant.USER_PART_NAME;
import static com.primb.bench.common.module.http.service.ApiClient.initService;

/**
 * Created by Chen on 2017/5/24.
 * 功能描述：
 */

public class LoanStockRepository {

    public static Observable<LoanCalculateInfoEntity> getLoanCalculateInfoEntity(String agmId, String sceneType, String priceType) {
//        Observable<HttpLogoEntity> logoEntityObservable = ApiClient.initService(ApiService.class).getLoanLogoInfo();
        Observable<HttpStockEntity> stockEntityObservable = ApiClient.initService(ApiService.class).stockCalculateLoan(agmId);
//        Observable<HttpInfoEntity> loanCurrencyList = ApiClient.initService(ApiService.class).getLoanCurrencyList();
//        Observable<HttpInfoEntity> loanIndustryList = ApiClient.initService(ApiService.class).getLoanIndustryList();
//        Observable<HttpInfoEntity> loanRateList = ApiClient.initService(ApiService.class).getLoanInfoList(AddInfoModelImpl.KEY_LOAN_RATE);
//        Observable<HttpInfoEntity> ensureMainList = ApiClient.initService(ApiService.class).getLoanInfoList(AddInfoModelImpl.KEY_ENSURE_MAIN);

//        stockEntityObservable.flatMap(new Func1<HttpStockEntity, Observable<LoanCalculateInfoEntity>>() {
//            @Override
//            public Observable<LoanCalculateInfoEntity> call(HttpStockEntity httpStockEntity) {
//                LoanCalculateInfoEntity infoEntity = new LoanCalculateInfoEntity();
//                if (httpStockEntity.isSuccess()) {
//
//                } else {
//                    Observable.error(new ApiException("数据请求失败"));
//                }
//                return Observable.just(infoEntity);
//            }
//        });
        return stockEntityObservable.flatMap(new Func1<HttpStockEntity, Observable<LoanCalculateInfoEntity>>() {
            @Override
            public Observable<LoanCalculateInfoEntity> call(HttpStockEntity httpStockEntity) {
                LoanCalculateInfoEntity infoEntity = new LoanCalculateInfoEntity();
                HttpStockEntity.DataBean dataBean = httpStockEntity.getData();
                LogoInfoEntity logoInfoEntity = new LogoInfoEntity();
                logoInfoEntity.setLogoDate(FormatUtils.DATE_FORMAT.format(new Date()));
                logoInfoEntity.setLogoManagerName(USER_NAME);
                logoInfoEntity.setLogoManagerNum(USER_NAME_ID);
                logoInfoEntity.setLogoNumber(dataBean.getPriceId());
                logoInfoEntity.setLogoPart(USER_PART_NAME);
                logoInfoEntity.setLogoPartId(USER_PART_ID);
                logoInfoEntity.setLogoProtocolType(sceneType);
                logoInfoEntity.setLogoPriceType(priceType);
                logoInfoEntity.setLogoProtocolNum(agmId);
                infoEntity.setLogoInfoEntity(logoInfoEntity);

                ClientInfoEntity clientInfoEntity = new ClientInfoEntity();
                clientInfoEntity.setCustomerName(dataBean.getCustName());
                clientInfoEntity.setCustomerNum(dataBean.getCustId());
                clientInfoEntity.setCustomerType(dataBean.getCustTypeDesc());
                clientInfoEntity.setCustomerTypeId(dataBean.getCustType());
                clientInfoEntity.setCustomerCredit(dataBean.getCreditRankCdDesc());
                clientInfoEntity.setCustomerCreditId(dataBean.getCreditRankCd());
                clientInfoEntity.setCustomerSize(dataBean.getLnsScopeCdDesc());
                clientInfoEntity.setCustomerSizeId(dataBean.getLnsScopeCd());
                clientInfoEntity.setCustomerContributionNum(dataBean.getNowRemainGx());
                clientInfoEntity.setCustomerAdjustRate(dataBean.getAdjustPd());
                clientInfoEntity.setCustomerIsContribution("0");
                //  clientInfoEntity.setCustomerNumM(dataBean.getCustIdM());
                //  clientInfoEntity.setCustomerContributionNumM(dataBean.getNowRemainGxM());
                //  clientInfoEntity.setCustomerUsedContributionM(dataBean.getNowUseGxM());
                clientInfoEntity.setCustomerAllContribution(dataBean.getNowSumGx());
                clientInfoEntity.setCustomerUsedContribution(dataBean.getNowUseGx());
                clientInfoEntity.setCustomerAllProfit(dataBean.getNowSumGp());
                clientInfoEntity.setCustomerInfoEmpty(false);
                infoEntity.setClientInfoEntity(clientInfoEntity);

                LoanInfoEntity loanInfoEntity = new LoanInfoEntity();
                loanInfoEntity.setLoanMoney(dataBean.getLnAmt());
                loanInfoEntity.setLoanDate(dataBean.getTremCd());
                if (dataBean.getTremUnit().equals("0"))
                    loanInfoEntity.setLoanDateUnit("年");
                else
                    loanInfoEntity.setLoanDateUnit("月");
                loanInfoEntity.setLoanRepayment(dataBean.getRepayPatnCdDesc());
                loanInfoEntity.setLoanRepaymentId(dataBean.getRepayPatnCd());
                loanInfoEntity.setLoanType(dataBean.getLnCurDesc());
                loanInfoEntity.setLoanTypeId(dataBean.getLnCur());
                loanInfoEntity.setLoanBusinessType(dataBean.getProdName());
                loanInfoEntity.setLoanBusinessTypeId(dataBean.getProdCd());
                loanInfoEntity.setLoanIndustry(dataBean.getLnInduDesc());
                loanInfoEntity.setLoanIndustryId(dataBean.getLnIndu());
                loanInfoEntity.setLoanRateType(dataBean.getLnIntTypeDesc());
                loanInfoEntity.setLoanRateTypeId(dataBean.getLnIntType());
                if (dataBean.getLnCur().equals("") || dataBean.getProdName().equals("") ||
                        dataBean.getProdCd().equals("") || dataBean.getLnIndu().equals("") ||
                        dataBean.getLnIntType().equals(""))
                    loanInfoEntity.setLoanInfoEmpty(true);
                else
                    loanInfoEntity.setLoanInfoEmpty(false);
                infoEntity.setLoanInfoEntity(loanInfoEntity);

                EnsureInfoEntity ensureInfoEntity = new EnsureInfoEntity();
                ensureInfoEntity.setEnsureMainTypeId(dataBean.getLnAssType());
                ensureInfoEntity.setEnsureMainType(dataBean.getLnAssTypeDesc());
                ensureInfoEntity.setEnsureConcretenessType(dataBean.getDetailAssName());
                ensureInfoEntity.setEnsureConcretenessTypeId(dataBean.getDetailAssType());

                ensureInfoEntity.setCommonGuarantee(dataBean.getCommonGuaranteeAmt());
                ensureInfoEntity.setLnOthMortAmt(dataBean.getLnOthMortAmt());
                ensureInfoEntity.setLnAcctRecvAmt(dataBean.getLnAcctRecvAmt());
                ensureInfoEntity.setHighGuarantee(dataBean.getHighGuaranteeAmt());
                ensureInfoEntity.setLnPropertyAmt(dataBean.getLnPropertyAmt());
                ensureInfoEntity.setLnFinImpawnAmt(dataBean.getLnFinImpawnAmt());

                ensureInfoEntity.setEnsureInfoEmpty(false);
                infoEntity.setEnsureInfoEntity(ensureInfoEntity);

                TaxInfoEntity taxInfoEntity = new TaxInfoEntity();
                taxInfoEntity.setTaxVat(dataBean.getAddValueTax());
                taxInfoEntity.setTaxIncome(dataBean.getLnIncomeTax());
                infoEntity.setTaxInfoEntity(taxInfoEntity);
                return Observable.just(infoEntity);
            }
        }).compose(RxSchedulerUtils.normalSchedulersTransformer());
//        return Observable.zip(logoEntityObservable, stockEntityObservable,
//                ensureMainList, loanCurrencyList,
//                loanIndustryList, loanRateList, (httpLogoEntity, httpStockEntity, ensureMainEntity, currencyEntity, industryEntity, rateEntity) -> {
//                    LoanCalculateInfoEntity infoEntity = new LoanCalculateInfoEntity();
//                    if (checkSuccess(httpStockEntity.getSuccess())
//                            && httpLogoEntity.isSuccess()
//                            && ensureMainEntity.getData().size() > 0
//                            && currencyEntity.getData().size() > 0
//                            && industryEntity.getData().size() > 0
//                            && rateEntity.getData().size() > 0) {
//                        HttpLogoEntity.DataBean logoBean = httpLogoEntity.getData();
//                        LogoInfoEntity logoInfoEntity = new LogoInfoEntity();
//                        logoInfoEntity.setLogoDate(FormatUtils.DATE_FORMAT.format(new Date()));
//                        logoInfoEntity.setLogoManagerName(USER_NAME);
//                        logoInfoEntity.setLogoManagerNum(USER_NAME_ID);
//                        logoInfoEntity.setLogoNumber(logoBean.getPriceId());
//                        logoInfoEntity.setLogoPart(USER_PART_NAME);
//                        logoInfoEntity.setLogoPartId(USER_PART_ID);
//                        logoInfoEntity.setLogoProtocolType(sceneType);
//                        logoInfoEntity.setLogoPriceType(priceType);
//                        logoInfoEntity.setLogoProtocolNum(agmId);
//                        infoEntity.setLogoInfoEntity(logoInfoEntity);
//
//                        HttpStockEntity.MsgBean dataBean = httpStockEntity.getMsg().get(0);
//                        ClientInfoEntity clientInfoEntity = new ClientInfoEntity();
//                        clientInfoEntity.setCustomerName(dataBean.getCustName());
//                        clientInfoEntity.setCustomerNum(dataBean.getCustId());
//                        clientInfoEntity.setCustomerType(dataBean.getCustTypeDesc());
//                        clientInfoEntity.setCustomerTypeId(dataBean.getCustType());
//                        clientInfoEntity.setCustomerCredit(dataBean.getCreditRankCdDesc());
//                        clientInfoEntity.setCustomerCreditId(dataBean.getCreditRankCd());
//                        clientInfoEntity.setCustomerSize(dataBean.getLnsScopeCdDesc());
//                        clientInfoEntity.setCustomerSizeId(dataBean.getLnsScopeCd());
//                        clientInfoEntity.setCustomerContributionNum(dataBean.getNowRemainGx());
//                        clientInfoEntity.setCustomerAdjustRate(dataBean.getAdjustPd());
//                        clientInfoEntity.setCustomerIsContribution("0");
//                        //  clientInfoEntity.setCustomerNumM(dataBean.getCustIdM());
//                        //  clientInfoEntity.setCustomerContributionNumM(dataBean.getNowRemainGxM());
//                        //  clientInfoEntity.setCustomerUsedContributionM(dataBean.getNowUseGxM());
//                        clientInfoEntity.setCustomerAllContribution(dataBean.getNowSumGx());
//                        clientInfoEntity.setCustomerUsedContribution(dataBean.getNowUseGx());
//                        clientInfoEntity.setCustomerAllProfit(dataBean.getNowSumGp());
//                        clientInfoEntity.setCustomerInfoEmpty(false);
//                        infoEntity.setClientInfoEntity(clientInfoEntity);
//
//                        LoanInfoEntity loanInfoEntity = new LoanInfoEntity();
//                        loanInfoEntity.setLoanMoney(dataBean.getLnAmt());
//                        loanInfoEntity.setLoanDate(dataBean.getTremCd());
//                        if (dataBean.getTremUnit().equals("0"))
//                            loanInfoEntity.setLoanDateUnit("年");
//                        else
//                            loanInfoEntity.setLoanDateUnit("月");
//                        loanInfoEntity.setLoanType(getVale(dataBean.getLnCur(), currencyEntity));
//                        loanInfoEntity.setLoanTypeId(dataBean.getLnCur());
//                        loanInfoEntity.setLoanBusinessType(dataBean.getProdName());
//                        loanInfoEntity.setLoanBusinessTypeId(dataBean.getProdCd());
//                        loanInfoEntity.setLoanIndustry(getVale(dataBean.getLnIndu(), industryEntity));
//                        loanInfoEntity.setLoanIndustryId(dataBean.getLnIndu());
//                        loanInfoEntity.setLoanRateType(getVale(dataBean.getLnIntType(), rateEntity));
//                        loanInfoEntity.setLoanRateTypeId(dataBean.getLnIntType());
//                        if (dataBean.getLnCur().equals("") || dataBean.getProdName().equals("") ||
//                                dataBean.getProdCd().equals("") || dataBean.getLnIndu().equals("") ||
//                                dataBean.getLnIntType().equals(""))
//                            loanInfoEntity.setLoanInfoEmpty(true);
//                        else
//                            loanInfoEntity.setLoanInfoEmpty(false);
//                        infoEntity.setLoanInfoEntity(loanInfoEntity);
//
//                        EnsureInfoEntity ensureInfoEntity = new EnsureInfoEntity();
//                        ensureInfoEntity.setEnsureMainTypeId(dataBean.getLnAssType());
//                        ensureInfoEntity.setEnsureMainType(getVale(dataBean.getLnAssType(), ensureMainEntity));
//                        ensureInfoEntity.setEnsureConcretenessType(dataBean.getDetailAssName());
//                        ensureInfoEntity.setEnsureConcretenessTypeId(dataBean.getDetailAssType());
//
////                        ensureInfoEntity.setCommonGuarantee(dataBean.getCommonGuarantee());
////                        ensureInfoEntity.setLnOthMortAmt(dataBean.getLnOthMortAmt());
////                        ensureInfoEntity.setLnAcctRecvAmt(dataBean.getLnAcctRecvAmt());
////                        ensureInfoEntity.setHighGuarantee(dataBean.getHighGuarantee());
////                        ensureInfoEntity.setLnPropertyAmt(dataBean.getLnPropertyAmt());
////                        ensureInfoEntity.setLnFinImpawnAmt(dataBean.getLnFinImpawnAmt());
//
//                        ensureInfoEntity.setEnsureInfoEmpty(false);
//                        infoEntity.setEnsureInfoEntity(ensureInfoEntity);
//
//                        TaxInfoEntity taxInfoEntity = new TaxInfoEntity();
//                        taxInfoEntity.setTaxVat(logoBean.getAddValueTax());
//                        taxInfoEntity.setTaxIncome(logoBean.getLnIncomeTax());
//                        infoEntity.setTaxInfoEntity(taxInfoEntity);
//                    } else {
//                        Observable.error(new ApiException("请求服务器数据失败"));
//                    }
//                    return infoEntity;
//                }).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    private static String getVale(String id, HttpInfoEntity entity) {
        if (id != null && id.equals("")) {
            return "";
        }
        for (int i = 0; i < entity.getData().size(); i++) {
            if (id.equals(entity.getData().get(i).getCode())) {
                return entity.getData().get(i).getDesc();
            }
        }
        return id;
    }

    private static boolean checkSuccess(String success) {
        if (success.equals("true")) {
            return true;
        }
        return false;
    }
}
